<?php


namespace app\common\model;


use app\common\enum\MainEnum;

class OrderListRefundModel extends CommonModel
{
    protected $name = 'order_list_refund';

    public function getQsStatusAttr($v): string
    {
        foreach (MainEnum::QS_STATUS as $key => $value) {
            if ($v === $value) {
                return $key;
            }
        }
        return '';
    }

    public function setApproveTimeAttr($timestamp)
    {
        return date('Y-m-d H:i:s', $timestamp);
    }

    public function setRefuseTimeAttr($timestamp)
    {
        return date('Y-m-d H:i:s', $timestamp);
    }

    public function getTkTypeAttr($v): string
    {
        foreach (MainEnum::QK_TYPE as $key => $value) {
            if ($v === $value) {
                return $key;
            }
        }
        return '';
    }

    public function getReasonDescImgUrlAttr($v)
    {
        return explode('|', $v);
    }

    public function setReasonDescImgUrlAttr($v)
    {
        return implode('|', $v);
    }

    public function getIsDefaultAttr($v): bool
    {
        return $v === 1;
    }

    public function setIsDefaultAttr($v): int
    {
        return $v === true ? 1 : 0;
    }

    public function setStatusAttr($value, $data)
    {
        (new OrderListModel())->where('id', $data['order_list_id'])->save(['status' => $value]);
        if ($value === MainEnum::ORDER_LIST_REFUND_DONE) {
            (new UserOrderBrokerageModel())->where('order_list_id', $data['order_list_id'])
                ->save(['status' => MainEnum::USER_ORDER_BROKERAGE_STATUS['已退款']]);
        }
        return $value;
    }

    public function orderInfo(): \think\model\relation\hasOne
    {
        return $this->hasOne(OrderModel::class, 'nb', 'order_nb');
    }

    public function userInfo(): \think\model\relation\hasOne
    {
        return $this->hasOne(UserModel::class, 'id', 'user_id');
    }

    public function orderListInfo(): \think\model\relation\hasOne
    {
        return $this->hasOne(OrderListModel::class, 'id', 'order_list_id');
    }

    public function getExtendStatusTextAttr($v, $data): ?string
    {
        return self::getOrderListRefundStatusText($data['status']);
    }

    public static function getOrderListRefundStatusText($status): ?string
    {
        switch ($status) {
            case MainEnum::ORDER_LIST_REFUND_WAIT:
                return '未申请';
            case MainEnum::ORDER_LIST_REFUND_ING:
                return '申请退款中';
            case MainEnum::ORDER_LIST_REFUND_DAILURU:
                return '待录入退货信息';
            case MainEnum::ORDER_LIST_REFUND_DAISHOUHUO:
                return '待卖家签收并验货';
            case MainEnum::ORDER_LIST_REFUND_DAIYANHUO:
                return '卖家已签收待验货';
            case MainEnum::ORDER_LIST_REFUND_WAIT_PAY:
                return '已验货，待回款';
            case MainEnum::ORDER_LIST_REFUND_DONE:
                return '已退款';
            case MainEnum::ORDER_LIST_REFUND_REFUSE:
                return '已拒绝退款';
            default:
                return '';
        }
    }

    public function getRefund($where = [])
    {
        return $this->with([
            'userInfo',
            'orderInfo',
            'orderListInfo',
        ])->order([
            'create_time' => 'desc',
        ])->where($where)->append([
            'extend_status_text',
            'orderInfo.extend_status_text',
        ])->find();
    }

    public function getRefundList($where = [], $page = 1, $limit = 10)
    {
        return $this->with([
            'userInfo',
            'orderInfo',
            'orderListInfo',
        ])->order([
            'create_time' => 'desc',
        ])->where($where)->append([
            'extend_status_text',
            'orderInfo.extend_status_text',
        ])->paginate([
            'page'      => $page,
            'list_rows' => $limit,
        ]);
    }
}